package fr.cephb.j2ee.tag;

import java.io.IOException;
import java.util.regex.Pattern;

import javax.servlet.jsp.JspWriter;


public class  AcnTag
	extends AbstractContentTag
    {
    private final Pattern RS_PATTERN=Pattern.compile("rs[1-9][0-9]*");
	private final Pattern ACN_PATTERN=Pattern.compile("(NM|NP)_[0-9]+");
	private final Pattern ACN_PATTERN2=Pattern.compile("[A-Z][0-9A-Z]{5,7}(\\.[0-9]+)?");
	
    private static final long serialVersionUID = 1L;
    public AcnTag()
            {
            }
    
    private void printAnchor(JspWriter out,String uri,String title,String content)
    throws IOException
    	{
    	out.print("<a href='"+uri+"'");
    	if(title!=null) out.print(" title='"+title+"'");
    	out.print(">");
    	out.print(content);
    	out.print("</a>");
    	}
    
    @Override
    public void print(JspWriter out, String content) throws IOException
            {
            
            if(RS_PATTERN.matcher(content).matches())
    			{
    			printAnchor(
    				out,
    			    "http://www.ncbi.nlm.nih.gov/projects/SNP/snp_ref.cgi?rs="+content,
    			    content,
    			    content
    			    );
    			}
            else if(ACN_PATTERN.matcher(content).matches())
    			{
    			if(content.startsWith("NP_"))
    				{
    				printAnchor(
    	    				out,
    	    				"http://www.ncbi.nlm.nih.gov/sites/entrez?db=protein&amp;cmd=search&amp;term="+content,
    	    			    content,
    	    			    content
    	    			    );
    				}
    			else if(content.startsWith("NM_"))
    				{
    				printAnchor(
    	    				out,
    	    				"http://www.ncbi.nlm.nih.gov/sites/entrez?db=nuccore&amp;cmd=search&amp;term="+content,
    	    			    content,
    	    			    content
    	    			    );
    				}
    			}
            else if(ACN_PATTERN2.matcher(content).matches())
    			{
    			printAnchor(
					out,"http://www.ncbi.nlm.nih.gov/sites/entrez?db=nuccore&amp;cmd=search&amp;term="+content,
					content,
				    content
				    );
    			}
            else
            	{
	    		out.print(content);
	            }
            }


    }

